<?php

function getFixtureUpToRound($leagueId, $round, $isArchive) {
	$sql = "select homeTeamId, awayTeamId, gameNumber, roundId from fixture" . getTableExtension($isArchive) . " f inner join round" . getTableExtension($isArchive) . " r on f.roundId = r.id where leagueId = $leagueId and round <= $round";
	$result = mysql_query($sql);
	
	$fixtureRounds = array();
	$currentRound = -1;
	$fixtureRound = null;
	while($row = mysql_fetch_array($result)) {
		$fixture = new Fixture();
		$fixture->homeTeamId = $row["homeTeamId"];
		$fixture->awayTeamId = $row["awayTeamId"];
		$fixture->gameNumber = $row["gameNumber"];
		$fixture->leagueId = $leagueId;
		$fixture->round = $row["roundId"];
		$array[] = $fixture;
		if ($currentRound != $fixture->round) {
			if ($fixtureRound != null) {
				$fixtureRounds[] = $fixtureRound;
			}
			$fixtureRound = new FixtureRound();
			$fixtureRound->fixtures = array();
			$fixtureRound->round = $fixture->round;
			$currentRound = $fixture->round;
		}
		
		$fixtureRound->fixtures[] = $fixture;
	}
	
	$fixtureRounds[] = $fixtureRound;
	
	
	return $fixtureRounds;
}

function getFixture($roundId, $leagueId, $isArchive) {
	$sql = "select * from fixture" . getTableExtension($isArchive) . " where roundId = $roundId and leagueId = $leagueId";
	$result = mysql_query($sql);
	
	$array = array();
	while($row = mysql_fetch_array($result)) {
		$fixture = new Fixture();
		$fixture->homeTeamId = $row['homeTeamId'];
		$fixture->awayTeamId = $row['awayTeamId'];
		$fixture->gameNumber = $row['gameNumber'];
		$fixture->leagueId = $row['leagueId'];
		$fixture->round = $row['roundId'];
		
		$array[] = $fixture;
	}
	
	
	return $array;
}

function getCurrentFixture($leagueId) {
	$sql = "select t1.id as homeTeamId, t1.teamName as homeTeamName, t2.id as awayTeamId, t2.teamName as awayTeamName, f.round, gameNumber from fixture f inner join team t1 on f.homeTeamId = t1.id";
	$sql .= " inner join team t2 on f.awayTeamId = t2.id inner join round r on f.round = r.round where f.leagueId = $leagueId order by round, gameNumber";
	
	$result = mysql_query($sql);
	
	$array = array();
	while($row = mysql_fetch_array($result)) {
		$fixtureItem = new FixtureItem();
		$fixtureItem->round = $row['round'];
		$fixtureItem->homeTeamName = $row['homeTeamName'];
		$fixtureItem->awayTeamName = $row['awayTeamName'];
		$fixtureItem->homeTeamId = $row['homeTeamId'];
		$fixtureItem->awayTeamId = $row['awayTeamId'];
		$fixtureItem->gameNumber = $row['gameNumber'];
		$fixtureItem->homePlayerScores = getRoundScoreForTeam($fixtureItem->homeTeamId, $fixtureItem->round, $leagueId);
		$fixtureItem->awayPlayerScores = getRoundScoreForTeam($fixtureItem->awayTeamId, $fixtureItem->round, $leagueId);
		
		$array[] = $fixtureItem;
	}
	
	
	return $array;
}

function getRoundResults($leagueId, $round) {
	$sql = "select t1.id as homeTeamId, t1.teamName as homeTeamName, t2.id as awayTeamId, t2.teamName as awayTeamName, round, gameNumber from fixture f inner join team t1 on f.homeTeamId = t1.id";
	$sql .= " inner join team t2 on f.awayTeamId = t2.id where f.leagueId = $leagueId and round = $round order by gameNumber";
	$result = mysql_query($sql);
	
	$array = array();
	while($row = mysql_fetch_array($result)) {
		$fixtureItem = new FixtureItem();
		$fixtureItem->round = $row['round'];
		$fixtureItem->homeTeamName = $row['homeTeamName'];
		$fixtureItem->awayTeamName = $row['awayTeamName'];
		$fixtureItem->gameNumber = $row['gameNumber'];
		$fixtureItem->homePlayerScores = getRoundScoreForTeam($row['homeTeamId'], $fixtureItem->round, $leagueId);
		$fixtureItem->awayPlayerScores = getRoundScoreForTeam($row['awayTeamId'], $fixtureItem->round, $leagueId);
		
		$array[] = $fixtureItem;
	}
	
	
	return $array;
}
?>
